<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        #app{
            width: 400px;
            height: 400px;
            background-color:aquamarine;
            margin: auto;
            text-align: center;
        }

        ul{
            margin: 0;
            padding: 0;
            margin-top: 20px;
            width: 100%;
            /* background-color: lightcoral; */
        }
        li{
            width: 100%;
            height: 28px;
            line-height: 28px;
            margin: auto;
            display: flex;
            background-color: lightgray;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        h2{
            text-align: center;
            font-size: 16px;
        }
        input:focus::placeholder{
            opacity: 0;
        }


    </style>
</head>
<body>
    <div id="app">
        <input type="text" @keyup.enter="add_input" placeholder="请添加每日任务"><button @click="add_input">添加</button>
        <ul>
            <h2 v-if="all_tasks.length == 0">当前任务为空</h2>
            <li v-for="(item,index) in all_tasks" :key="item.index">{{item.msg}} <button @click="del(index)">删除</button> </li>
        </ul>
    </div>

    <script>
        let app = new Vue({
            el:"#app",
            data:{
                msg:"",
                all_tasks:[]   
            },
            methods:{
                add_input(e){
                    console.log(e.target.value);
                    // console.log(e.target.previousSibling.value);
                    this.msg = e.target.value || e.target.previousSibling.value;
                    this.all_tasks.push({
                        "msg" : this.msg
                    })
                    if(e.target.value){
                        e.target.value=""
                    }else{
                        e.target.previousSibling.value=""
                    }
                    console.log(this.all_tasks);
                },
                del(index){
                    console.log("点击了删除按钮");
                    this.all_tasks.splice(index,1)
                }
            }
        })
    </script>
</body>
</html>